package com.haitaoit.chengquan.module.center_service.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.center_service.model.QualificationModel;
import com.haitaoit.chengquan.ui.equipment.activity.ShowPictureActivity;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/10/18.
 */
public class QualificationAdapter extends BaseAdapter {
    private Context context;
    private BackCall backCall;
    private List<QualificationModel> list;

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public QualificationAdapter(Context context) {
        this.context = context;
    }

    public List<QualificationModel> getList() {
        return list;
    }

    public void setList(List<QualificationModel> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.qualification_item, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.quali_detele:
                        QualificationAdapter.this.backCall.deal(R.id.quali_detele, position);
                        break;
                    case R.id.quali_ll:
                    case R.id.quali_name:
                    case R.id.quali_time:
                        holder.quali_cb.setChecked(!holder.quali_cb.isChecked());
                        QualificationAdapter.this.backCall.deal(R.id.quali_ll, position);
                        break;
                }
            }
        };
        holder.quali_detele.setOnClickListener(listener);
        holder.quali_ll.setOnClickListener(listener);
        holder.quali_name.setOnClickListener(listener);
        holder.quali_time.setOnClickListener(listener);

        holder.quali_cb.setChecked(false);
        ImageLoader.getInstance().displayImage(list.get(position).getImg_url(), holder.quali_img);
        holder.quali_name.setText(list.get(position).getName());
        holder.quali_time.setText("上传时间：" + list.get(position).getAddtime());

        holder.quali_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(parent.getContext(), ShowPictureActivity.class);
                intent.putExtra("image_uri", list.get(position).getImg_url());
                parent.getContext().startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.quali_detele)
        ImageView quali_detele;
        @ViewInject(R.id.quali_img)
        ImageView quali_img;
        @ViewInject(R.id.quali_ll)
        LinearLayout quali_ll;
        @ViewInject(R.id.quali_cb)
        CheckBox quali_cb;
        @ViewInject(R.id.quali_name)
        TextView quali_name;
        @ViewInject(R.id.quali_time)
        TextView quali_time;
    }
}
